package com.evolved.automata.experimental.speech;import java.util.*;
import java.util.concurrent.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.*;
import java.io.*;
import javax.swing.*;
import java.util.regex.*;

public class TestHarness {
	public enum Message
	{
		TIMEOUT,WORD;
	}
	
	static BlockingQueue<Object> speechQueue;
	public static void main(String[] args) throws Exception
	{
		String configLocation = "speech.config9.xml";
		String inputFileFullName="speech.config7.xml";
		BufferedReader bfReader=null;
		try
		{
			if ((new File(inputFileFullName)).exists())
			{
				bfReader = new BufferedReader(new FileReader(inputFileFullName));
				String lineInput="";
				while ((lineInput=bfReader.readLine())!=null)
				{
					System.out.println(lineInput);
				}
			}
			String patternString = "(%1$s,%2$s,%3$s)";
			java.util.GregorianCalendar cal = new GregorianCalendar();
			
			String formattedString = String.format(patternString,"Jack",12,cal.getTime().toLocaleString());
			System.out.println(formattedString);
			
		}
		catch (Exception e)
		{
			StringWriter  sWriter = new StringWriter();
			PrintWriter pWriter = new PrintWriter(sWriter,true);
			e.printStackTrace(pWriter);
			pWriter.close();
			throw new RuntimeException(sWriter.toString());
			
		}
		finally
		{
			if (bfReader!=null)
				bfReader.close();
		}
		
		Pattern p=Pattern.compile("^\\-\\-.*",Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher("-  [StAteName Groups]");
		
		
		System.out.println(m.matches());
		
	}
	
	public static void StandardTest()
	{
		String configLocation = "speech.config.xml";
		speechQueue = new LinkedBlockingQueue<Object>();
		SpeechProcessor proc = new SpeechProcessor(speechQueue,configLocation);
		
		Runnable timeoutWatcher = new Runnable()
		{
			public void run()
			{
				System.out.println("timeout started");
				try
				{
					Thread.sleep(10*1000);
					speechQueue.put(Message.TIMEOUT);
					
				}
				catch (InterruptedException e)
				{
					
				}
				System.out.println("Dead");
			}
		};
		
		
		
		try
		{
			
			proc.StartProcessing();
			SpeechProcessor.SpeechResponse speechMessage;
			Thread timeoutThread=null;
			Object message;
			
			loop:
			for (int i=0;i<4;i++)
			{
				// Start timeout thread
				timeoutThread = new Thread(timeoutWatcher);
				System.out.println(i);
				timeoutThread.start();
				// Wait for next message
				message = speechQueue.take();
				if (message instanceof SpeechProcessor.SpeechResponse)
				{
					speechMessage =(SpeechProcessor.SpeechResponse)message;
					switch (speechMessage.getResponseType())
					{
						case EXCEPTION:
							System.out.println("An exception occurred");
							System.out.println("Speech processing has halted");
							System.out.println(speechMessage.getResponseString());
							break loop;
						default:
							System.out.println(speechMessage.getResponseString());
							break;
					}
					timeoutThread.interrupt();
					
				}
				else
				{
					System.out.println("Timeout occured before new word\r\n");
				}
			}
			System.out.println("Finished");
			proc.StopProcessing();
		}
		catch (Exception e)
		{
			StringWriter  sWriter = new StringWriter();
			PrintWriter pWriter = new PrintWriter(sWriter,true);
			e.printStackTrace(pWriter);
			pWriter.close();
			System.out.println(sWriter.toString());
		}
		//System.exit(0);
	}
}
